/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.states;

import com.jme3.texture.Texture;
import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.post.filters.BloomFilter;
import com.jme3.post.filters.FogFilter;
import etherblocks.engine.objects.BlockMaterial;
import etherblocks.texturepacks.*;
import etherblocks.texturepacks.packs.*;
import com.cubes.BlockTerrainControl;

/**
 *
 * @author Carl
 */
public class TexturePackAppState extends BaseAppState{

    public TexturePackAppState(){
        
    }
    public enum MaterialType{
        UNSHADED,
        LIGHTING
    }
    private MaterialType blockMaterialType;
    private TexturePack texturePack;

    @Override
    public void initialize(AppStateManager stateManager, Application application){
        super.initialize(stateManager, application);
        setBlockMaterialType(MaterialType.LIGHTING);
        setTexturePack(new DefaultTexturePack(2));
    }
    
    public void changeBlockMaterialType(){
        setBlockMaterialType(((blockMaterialType == MaterialType.UNSHADED)?MaterialType.LIGHTING:MaterialType.UNSHADED));
    }
    
    public void setBlockMaterialType(MaterialType blockMaterialType){
        this.blockMaterialType = blockMaterialType;
        Material blockMaterial = null;
        switch(blockMaterialType){
            case UNSHADED:
                blockMaterial = new Material(mainApplication.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
                break;
            
            case LIGHTING:
                blockMaterial = new BlockMaterial(mainApplication.getAssetManager());
                break;
        }
        blockMaterial.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
        getBlockTerrain().getSettings().setBlockMaterial(blockMaterial);
        getBlockTerrain().updateBlockMaterial();
        updateTerrainTexture();
    }

    public void setTexturePack(TexturePack texturePack){
        this.texturePack = texturePack;
        //Light
        mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(LightAppState.class));
        LightAppState lightAppState = new LightAppState();
        lightAppState.setAmbientLightColor(texturePack.getProperties().getColorRGBA("ambientLightColor"));
        lightAppState.setDirectionalLightColor(texturePack.getProperties().getColorRGBA("directionalLightColor"));
        lightAppState.setLightDirection(texturePack.getProperties().getVector3f("lightDirection"));
        lightAppState.setShadowsIntensity(texturePack.getProperties().getFloat("shadowsIntensity"));
        mainApplication.getStateManager().attach(lightAppState);
        //PostFilter
        mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(PostFilterAppState.class));
        PostFilterAppState postFilterAppState = new PostFilterAppState();
        if(texturePack.getProperties().getBoolean("isBloomEnabled")){
            BloomFilter bloomFilter = new BloomFilter();
            bloomFilter.setBloomIntensity(texturePack.getProperties().getFloat("bloomIntenstiy"));
            postFilterAppState.addFilter(bloomFilter);
        }
        if(texturePack.getProperties().getBoolean("isFogEnabled")){
            FogFilter fogFilter = new FogFilter();
            fogFilter.setFogColor(texturePack.getProperties().getColorRGBA("fogColor"));
            fogFilter.setFogDensity(texturePack.getProperties().getFloat("fogDensity"));
            fogFilter.setFogDistance(texturePack.getProperties().getFloat("fogDistance"));
            postFilterAppState.addFilter(fogFilter);
        }
        mainApplication.getStateManager().attach(postFilterAppState);
        //Sky
        mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(SkyAppState.class));
        SkyAppState skyAppState = new SkyAppState();
        skyAppState.setAnimationDuration(texturePack.getProperties().getInteger("skyAnimationDuration"));
        skyAppState.setColors(
            texturePack.getProperties().getColorRGBA("skyStartColorTop"),
            texturePack.getProperties().getColorRGBA("skyStartColorBottom"),
            texturePack.getProperties().getColorRGBA("skyEndColorTop"),
            texturePack.getProperties().getColorRGBA("skyEndColorBottom")
        );
        mainApplication.getStateManager().attach(skyAppState);
        //Clouds
        mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(CloudsAppState.class));
        mainApplication.getStateManager().attach(new CloudsAppState());
        //MotionBlur
        mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(MotionBlurAppState.class));
        mainApplication.getStateManager().attach(new MotionBlurAppState());
        updateTerrainTexture();
    }
    
    private void updateTerrainTexture(){
        if(texturePack != null){
            Texture texture = mainApplication.getAssetManager().loadTexture(texturePack.getTerrainTexturePath());
            texture.setMagFilter(Texture.MagFilter.Nearest);
            texture.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
            Material blockMaterial = getBlockTerrain().getSettings().getBlockMaterial();
            switch(blockMaterialType){
                case UNSHADED:
                    blockMaterial.setTexture("ColorMap", texture);
                    break;
                
                case LIGHTING:
                    blockMaterial.setTexture("DiffuseMap", texture);
                    break;
            }
        }
    }
    
    private BlockTerrainControl getBlockTerrain(){
        return getAppState(WorldAppState.class).getWorld().getBlockTerrain();
    }

    public TexturePack getTexturePack(){
        return texturePack;
    }
}
